import React, { Component } from 'react';
import {
    Table,
    Pagination
} from 'tinper-bee';

import Tooltip from 'bee-tooltip';
import 'bee-tooltip/build/Tooltip.css';
const textOverflowIndex = 10; // 控制项目名称在多少个字符后隐藏

import {
    vendorsFmtDate,
    vendorsDanger as danger,
} from "../../utils/config";

import {
    getBillStatusText,
    isOverTime,
    getPrintCode,
    getBoCode,
    getDiffReq,
    getPrintCodeByType
} from './billsInquiry.config';

import jdiwork from '../../utils/jdiwork'
let tenantId;
let cookieStr = document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1)
if (cookieStr) {
    tenantId = cookieStr.split('=')[1];
}
// let tenantId = "x61tnmfx";
class TableBill extends Component {
    constructor(props) {
        super(props);
        this.state = {
            columns: null,
            boCode: ''
        }
    }

    getColumnOfProjectName = (key) => {
        return {
            title: "项目名称", dataIndex: key, key, className: "text-overflow", width: 200,
            render(text, rec, index) {
                let displayText = text.length > textOverflowIndex ? text.substr(0, textOverflowIndex) + "..." : text;
                return (
                    <Tooltip
                        className={[text.length > textOverflowIndex ? "show" : "hidden"]}
                        inverse
                        placement="top"
                        overlay={text}>
                        <div>{displayText}</div>
                    </Tooltip>
                )
            }
        }
    }

    componentWillReceiveProps(nextProps) {
        let _this = this;
        let type = nextProps.type;
        let columns = null;
        switch (type) {
            // 开工报告
            case "KGBG":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "施工单位", dataIndex: "enterpriseName", key: "enterpriseName" },
                    { title: "监理单位", dataIndex: "supervisionName", key: "supervisionName" },
                    { title: "开工条件总数", dataIndex: "workqualityCount", key: "workqualityCount" },
                    { title: "不具备项数", dataIndex: "checkCount", key: "checkCount" },
                    { title: "开工日期", dataIndex: "startdate", key: "startdate" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "制单人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ];
                break;
            // 安全整改
            case "AQZG":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "整改区域", dataIndex: "rectificationArea", key: "rectificationArea" },
                    { title: "施工单位", dataIndex: "constructUnitName", key: "constructUnitName" },
                    { title: "问题原因", dataIndex: "problemReasonName", key: "problemReasonName" },
                    { title: "严重程度", dataIndex: "strSeverityLevel", key: "strSeverityLevel" },
                    { title: "整改人", dataIndex: "rectificationerName", key: "rectificationerName" },
                    {
                        title: "是否超期", dataIndex: "overTime", key: "overTime",
                        render(text, record, index) {
                            return (
                                <span className={text ? "error-word-color" : ""}>{isOverTime(text)}</span>
                            )
                        }
                    },
                    {
                        title: "整改期限", dataIndex: "deadline", key: "deadline",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },
                    { title: "验收状态", dataIndex: "strBillStatus", key: "strBillStatus" },
                    { title: "整改次数", dataIndex: "rectifyTimes", key: "rectifyTimes" },
                    { title: "照片张数", dataIndex: "picNum", key: "picNum" },
                    { title: "制单人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ];
                break;
            // 巡查记录
            case "XCJL":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "检查部位", dataIndex: "inspectarea", key: "inspectarea" },
                    { title: "工序", dataIndex: "process_name", key: "process_name" },
                    { title: "检查标准", dataIndex: "inspectstandard_name", key: "inspectstandard_name" },
                    { title: "施工单位", dataIndex: "construction_name", key: "construction_name" },
                    { title: "检查项总数", dataIndex: "totItemNum", key: "totItemNum" },
                    { title: "不合格项数", dataIndex: "unPassNum", key: "unPassNum" },
                    {
                        title: "合格率", dataIndex: "passRate", key: "passRate",
                        render(text, record, index) {
                            return (
                                <span>{text}%</span>
                            )
                        }
                    },
                    { title: "制单人", dataIndex: "creator_name", key: "creator_name" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ]
                break;
            // 旁站记录
            case "PZJL":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "检查部位", dataIndex: "inspectarea", key: "inspectarea" },
                    { title: "工序", dataIndex: "process_name", key: "process_name" },
                    { title: "检查标准", dataIndex: "inspectstandard_name", key: "inspectstandard_name" },
                    { title: "施工单位", dataIndex: "construction_name", key: "construction_name" },
                    {
                        title: "旁站时间", dataIndex: "PZJL_time", key: "PZJL_time",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(record['startDate'])}~{vendorsFmtDate(record['endDate'])}</span>
                            )
                        }
                    },
                    { title: "检查项总数", dataIndex: "totItemNum", key: "totItemNum" },
                    { title: "不合格项数", dataIndex: "unPassNum", key: "unPassNum" },
                    {
                        title: "合格率", dataIndex: "passRate", key: "passRate",
                        render(text, record, index) {
                            return (
                                <span>{text}%</span>
                            )
                        }
                    },
                    { title: "制单人", dataIndex: "creator_name", key: "creator_name" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ]
                break;
            // 平行检验
            case "PXJY":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "检查部位", dataIndex: "inspectarea", key: "inspectarea" },
                    { title: "工序", dataIndex: "process_name", key: "process_name" },
                    { title: "检查标准", dataIndex: "inspectstandard_name", key: "inspectstandard_name" },
                    { title: "施工单位", dataIndex: "construction_name", key: "construction_name" },
                    { title: "检查项总数", dataIndex: "totItemNum", key: "totItemNum" },
                    { title: "不合格项数", dataIndex: "unPassNum", key: "unPassNum" },
                    {
                        title: "合格率", dataIndex: "passRate", key: "passRate",
                        render(text, record, index) {
                            return (
                                <span>{text}%</span>
                            )
                        }
                    },
                    { title: "制单人", dataIndex: "creator_name", key: "creator_name" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ]
                break;
            // 施工记录
            case "SGJL":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "检查部位", dataIndex: "inspectarea", key: "inspectarea" },
                    { title: "工序", dataIndex: "process_name", key: "process_name" },
                    { title: "检查标准", dataIndex: "inspectstandard_name", key: "inspectstandard_name" },
                    { title: "施工单位", dataIndex: "construction_name", key: "construction_name" },
                    { title: "检查项总数", dataIndex: "totItemNum", key: "totItemNum" },
                    { title: "不合格项数", dataIndex: "unPassNum", key: "unPassNum" },
                    {
                        title: "合格率", dataIndex: "passRate", key: "passRate",
                        render(text, record, index) {
                            return (
                                <span>{text}%</span>
                            )
                        }
                    },
                    { title: "审批状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "制单人", dataIndex: "creator_name", key: "creator_name" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ]
                break;
            // 工序报验
            case "GXBY":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "工序名称", dataIndex: "procedure_name", key: "procedure_name" },
                    { title: "施工单位", dataIndex: "constructionunit_name", key: "constructionunit_name" },
                    { title: "报验施工记录单的单数", dataIndex: "bodyNum", key: "bodyNum" },
                    { title: "报验日期", dataIndex: "inspect_date", key: "inspect_date" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "制单人", dataIndex: "creatorname", key: "creatorname" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ]
                break;
            // 问题记录
            case "WTJL":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "工序名称", dataIndex: "process_name", key: "process_name" },
                    { title: "问题原因名称", dataIndex: "problemreason_name", key: "problemreason_name" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    {
                        title: "完成日期", dataIndex: "enddate", key: "enddate",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },
                    {
                        title: "是否超期", dataIndex: "overTime", key: "overTime",
                        render(text, record, index) {
                            return (
                                <span className={text ? "error-word-color" : ""}>{isOverTime(text)}</span>
                            )
                        }
                    },
                    { title: "问题单照片张数", dataIndex: "picNum", key: "picNum" },
                    { title: "整改人", dataIndex: "rectificationer_name", key: "rectificationer_name" },
                    { title: "制单人", dataIndex: "creator_name", key: "creator_name" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ]
                break;
            // 工序拍照
            case "GXPZ":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "工序名称", dataIndex: "processName", key: "processName" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "拍摄人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "拍摄时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },
                    { title: "照片张数", dataIndex: "picNum", key: "picNum" }
                ]
                break;
            // 工作联系
            case "GZLX":
                columns = [
                    _this.getColumnOfProjectName("project_name"),
                    { title: "联系类型", dataIndex: "contype_name", key: "contype_name" },
                    { title: "工序名称", dataIndex: "process_name", key: "process_name" },
                    { title: "标题", dataIndex: "title", key: "title" },
                    { title: "接收状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "发起人", dataIndex: "creator_name", key: "creator_name" },
                    {
                        title: "接收人", dataIndex: "receiverNames", key: "receiverNames",
                        render(text, record, index) {
                            let receiverNamesStr = text.toString();
                            return (
                                <span>{receiverNamesStr}</span>
                            )
                        }
                    },
                    { title: "照片张数", dataIndex: "picNum", key: "picNum" },
                    {
                        title: "发起日期", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },
                ];
                break;
            // 签证申请
            case "BGQZ":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "施工单位", dataIndex: "enterpriseName", key: "enterpriseName" },
                    { title: "签证类别", dataIndex: "categoryName", key: "categoryName" },
                    { title: "预估金额", dataIndex: "strEstimateMny", key: "strEstimateMny" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "发起人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "申请日期", dataIndex: "applyDate", key: "applyDate",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },
                    { title: "照片张数", dataIndex: "picNum", key: "picNum" },
                ];
                break;
            // 变更申请
            case "BGSQ":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "发起单位", dataIndex: "enterpriseName", key: "enterpriseName" },
                    { title: "变更类别", dataIndex: "categoryName", key: "categoryName" },
                    { title: "预估金额", dataIndex: "strEstimateMny", key: "strEstimateMny" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "发起人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "申请日期", dataIndex: "applyDate", key: "applyDate",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },
                    { title: "照片张数", dataIndex: "picNum", key: "picNum" },
                ];
                break;
            // 施工日志
            case "SGRZ":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "日志内容", dataIndex: "diaryContent", key: "diaryContent" },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "发起人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "发起时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    },

                    { title: "照片张数", dataIndex: "picNum", key: "picNum" },
                ];
                break;
            // 综合验收
            case "ZHYS":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "检查标准", dataIndex: "inspectstandardName", key: "inspectstandardName" },
                    { title: "施工单位", dataIndex: "constructionName", key: "constructionName" },
                    { title: "检查项总数", dataIndex: "totItemNum", key: "totItemNum" },
                    { title: "不合格项数", dataIndex: "unPassNum", key: "unPassNum" },
                    {
                        title: "合格率", dataIndex: "passRate", key: "passRate",
                        render(text, record, index) {
                            return (
                                <span>{text}%</span>
                            )
                        }
                    },
                    { title: "单据状态", dataIndex: "strBillstatus", key: "strBillstatus" },
                    { title: "制单人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ];
                break;
            // 停工单
            case "TGBG":
                columns = [
                    _this.getColumnOfProjectName("projectName"),
                    { title: "停工日期", dataIndex: "stopDate", key: "stopDate" },
                    { title: "停工天数", dataIndex: "workstopdays", key: "workstopdays" },
                    { title: "预计复工日期", dataIndex: "preresumeDate", key: "preresumeDate" },
                    { title: "停工原因", dataIndex: "workstopreason", key: "workstopreason" },
                    { title: "照片张数", dataIndex: "picNum", key: "picNum" },
                    { title: "制单人", dataIndex: "creatorName", key: "creatorName" },
                    {
                        title: "制单时间", dataIndex: "creationtime", key: "creationtime",
                        render(text, record, index) {
                            return (
                                <span>{vendorsFmtDate(text)}</span>
                            )
                        }
                    }
                ];
                break;
            default:
                break;
        }
        // 抽取公共列
        let indexColumn = {
            title: "序号", dataIndex: "index", key: "index",
            render(text, record, index) {
                return <span>{index + 1}</span>
            }
        }
        let operateColumn = {
            title: "操作",
            dataIndex: "operate",
            key: "operate",
            render(text, record, index) {
                let param = getDiffReq(nextProps.type, record);
                return (
                    <div style={{ position: 'relative' }} title={text} >
                        <span
                            className="click-word-color"
                            target="_blank"
                            onClick={() => {
                                if (tenantId) {
                                    _this.preViewBill(param, record);
                                } else {
                                    jdiwork().getContext((data) => {
                                        tenantId = data.tenantid
                                        _this.preViewBill(param, record);
                                    });
                                }

                            }}>
                            预览
                        </span>
                    </div>
                );
            }
        }
        if (columns !== null) {
            columns.unshift(indexColumn);
            columns.push(operateColumn);
            this.setState({
                columns: [...columns]
            });
        }
    }

    //预览
    preViewBill = (param, record) => {
        getPrintCodeByType({
            'isAjax': 1,
            ieStamp: new Date().getTime(),
            tenantId: tenantId,
            billType: param.billType,
            pkProcess: record.pk_process,
        }).then(function (response) {
            if (response.data.resultCode != 'busi_ex') {
                let data = response.data.result;
                let url = `./print/preview?serverUrl=/billView/queryBillDetail&printcode=${data}&tenantId=${tenantId}&sendType=2&params={"pkBill":"${param.pkBill}", "billType":"${param.billType}"}`;
                let win = window.open(encodeURI(url));
                win.location.reload();
                return;
            } else {
                danger({ 'text': response.data.exMsg });
            }
        });
    }

    // 分页选择
    handleSelectPage = (e) => {
        this.props.handleSelectPage(e);
    }

    render() {
        return (
            <div>
                <Table
                    className="margin-top-20"
                    columns={this.state.columns}
                    data={this.props.data}
                    height={43}
                    headerHeight={42}
                />
                {this.props.data.length !== 0 &&
                    <Pagination style={{ float: 'right', margin: '15px 0' }}
                        first
                        last
                        prev
                        next
                        boundaryLinks
                        items={this.props.items}
                        maxButtons={5}
                        activePage={this.props.activePage || 1} // 尝试解决某些特殊环境下没有值得情况
                        onSelect={this.handleSelectPage}
                    />
                }
            </div>
        );
    }
}

export default TableBill;